package com.gzxxgl.task;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import org.apache.log4j.Logger;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.cron4j.ITask;

public class GLGZThead implements ITask {
	private static Logger logger = Logger.getLogger(GLGZThead.class);

	@Override
	public void run() {
		// TODO Auto-generated method stub

		// 开始对数据的工龄工资计算
		java.util.List<Record> r = Db.find("select * from g_user_salary where isdelete = 0");
		java.util.List<Record> list = new ArrayList<>();
		String rzrq = "";
		double glgz = 0;

		for (Record o : r) {

			if (o != null && StrKit.notBlank(o.getStr("rzrq"))) {

				rzrq = o.getStr("rzrq");

				try {
					//核算工龄工资
					glgz = Seniority(rzrq);
					o.set("glgz", glgz);

					list.add(o);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

		}

		int[] ret = null;
		if (list.size() > 0) {
			ret = Db.batchUpdate("g_user_salary", "salaryid", list, 300);
		}
		int s = 0, l = 0;
		if (ret != null) {
			for (int i = 0; i < ret.length; i++) {
				if (ret[i] >= 0)
					s++;
				else if (ret[i] >= -2)
					s++;
				else
					l++;
			}
		}
		logger.error("更新成功，总" + ret.length + "条数据   更新 " + s + "行数据，丢失" + l + "行数据 ");
		System.out.println("更新成功，总" + ret.length + "条数据   更新 " + s + "行数据，丢失" + l + "行数据 ");
	}

	private static double Seniority(String date) throws ParseException {

		Integer yearTemp1 = 1, yearTemp2 = 4, yearTemp3 = 7, yearTemp4 = 10, yearTemp5 = 16, yearTemp6 = 20;

		double money1 = 20, money2 = 30, money3 = 50, money4 = 60, money5 = 80, money6 = 100;

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		Date rzrq = sdf.parse(date);

		Calendar cal = Calendar.getInstance();

		int nyear = cal.get(Calendar.YEAR);

		cal.setTime(rzrq);

		int ryear = cal.get(Calendar.YEAR);
		int rmonth = cal.get(Calendar.MONTH) + 1;

		Integer year = null;

		year = nyear - ryear;
		if (rmonth >= 7) {
			if (year > 0) {
				year--;
			}
		}

		if (year >= yearTemp1 && year < yearTemp2) {
			return money1 * year;
		} else if (year >= yearTemp2 && year < yearTemp3) {
			return money2 * year;
		} else if (year >= yearTemp3 && year < yearTemp4) {
			return money3 * year;
		} else if (year >= yearTemp4 && year < yearTemp5) {
			return money4 * year;
		} else if (year >= yearTemp5 && year < yearTemp6) {
			return money5 * year;
		} else if (year >= yearTemp6) {
			return money6 * year;
		} else {
			return 0;
		}

	}

	@Override
	public void stop() {
		// TODO Auto-generated method stub
		System.out.println("stop");
	}

	public static void main(String[] args) throws ParseException {
		System.out.println(Seniority("2001-08-17"));
	}
}
